package com.oscar.ehome.view.konwledge;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.ClassRoomBean;
import com.oscar.ehome.presenter.KnowelgePresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.konwledge.adapter.KnowStudyListAdapter;
import com.oscar.ehome.view.konwledge.view.KnowSudyView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.ClassRoomVideoActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/9/21
 * 我的资料
 */

public class MyStudyListActivity extends BasesActivity<KnowSudyView, KnowelgePresenter> implements KnowSudyView, PullToRefreshBase.OnRefreshListener,
        ProgressFrameLayout.AgainRequestListener, AdapterView.OnItemClickListener {

    /**
     * 返回
     */
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    /**
     * 返回
     */
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    /**
     * 标题
     */
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    /**
     *
     */
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    /**
     * list view
     */
    @BindView(R.id.mystudylist_listview)
    PullToRefreshListView mystudylistListview;
    /**
     * 无数据布局
     */
    @BindView(R.id.mystudylist_fragment)
    ProgressFrameLayout mystudylistFragment;

    KnowStudyListAdapter adapter;
    KnowelgePresenter presenter;
    private int page = 1;//页数
    private int page_size = 20;//每页的条数
    private String token = "";
    private String s = "1";
    private List<ClassRoomBean.ClassRoomListBean> dataList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mystudylist);
        ButterKnife.bind(this);
        setColor();
        initView();
        onCLicks();
        initData();
    }

    @Override
    protected KnowelgePresenter createPresenter() {
        presenter = new KnowelgePresenter(this);
        return presenter;
    }

    /**
     * 设置颜色
     */
    public void setColor() {
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(MyStudyListActivity.this).getSystemConfig().getColor()));
    }

    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleTopbar.setText("我的资料");
        token = SystemConfigFactory.getInstance(MyStudyListActivity.this).getSystemConfig().getToken();

        adapter = new KnowStudyListAdapter(MyStudyListActivity.this, dataList);
        mystudylistListview.setAdapter(adapter);
    }

    public void onCLicks() {
        mystudylistListview.setPullLoadEnabled(true);
        mystudylistListview.setOnRefreshListener(this);
        mystudylistFragment.setAgainRequestListener(this);
        mystudylistListview.setOnItemClickListener(this);
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {
            mystudylistFragment.stopRefresh();
            mystudylistListview.setVisibility(View.VISIBLE);
            presenter.getstudyList(token, s, "20");
            showLoading();
        } else {
            mystudylistFragment.setVisibility(View.VISIBLE);
            mystudylistFragment.setLoadFail();
            mystudylistListview.setVisibility(View.GONE);
        }
    }

    /**
     * 判断是否有网
     */
    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(MyStudyListActivity.this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @OnClick(R.id.title_layout_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void getknowStudyList(ClassRoomBean result) {
        dismissLoading();
        if (result != null) {
            if (page == 1) {
                dataList.clear();
            }
            dataList = result.getList();
            if (dataList.size() > 0) {
                mystudylistListview.setVisibility(View.VISIBLE);
                adapter.setList(dataList);
            } else {
                mystudylistListview.setVisibility(View.GONE);
                mystudylistFragment.setNoResult();
            }
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        ToastUtil.show(MyStudyListActivity.this, getString(R.string.erry));
        mystudylistFragment.setLoadFail();
        mystudylistListview.setVisibility(View.GONE);
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        ToastUtil.show(MyStudyListActivity.this, result);
        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(MyStudyListActivity.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(MyStudyListActivity.this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(MyStudyListActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            mystudylistFragment.setLoadFail();
            mystudylistListview.setVisibility(View.GONE);
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        s = String.valueOf(page);
        initData();
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(MyStudyListActivity.this, getString(R.string.pull_to_refresh_network_error));
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (dataList.size() > 0) {
            ClassRoomBean.ClassRoomListBean bean = dataList.get(position);
            String addapp = new Gson().toJson(bean);
            Intent intent = new Intent(MyStudyListActivity.this, ClassRoomVideoActivity.class);
            intent.putExtra("messagebean", addapp);
            intent.putExtra("lawyer", 0);
            startActivity(intent);
        }
    }
}
